import Vue from 'vue'
import VueRouter from 'vue-router'

// 路由懒加载
const Login = () => import('../components/Login.vue')
const Home = () => import('../components/Home.vue')
const ProductList = () => import('../components/productList.vue') 
const ProductRentalAndSales = () => import('../components/productRentalAndSales.vue') 
const SessionGenerationDesignPrinciples = () => import('../components/sessionGenerationDesignPrinciples.vue') 
const VenueManagement = () => import('../components/venueManagement.vue') 
const VenueReservation = () => import('../components/venueReservation.vue') 
const MainOrder = () => import('../components/mainOrder.vue') 
const RentalAndSalesOrder = () => import('../components/rentalAndSalesOrder.vue') 
const VenueReservationOrder = () => import('../components/venueReservationOrder.vue') 
const MembershipCardPurchaseOrder = () => import('../components/membershipCardPurchaseOrder.vue') 
const RefundManagement = () => import('../components/refundManagement.vue') 
const CouponManagement = () => import('../components/couponManagement.vue') 
const MembershipCardManagement = () => import('../components/membershipCardManagement.vue') 
const MembershipCardList = () => import('../components/membershipCardList.vue') 
const FundsFlow = () => import('../components/fundsFlow.vue') 
const UserList = () => import('../components/userList.vue') 
const DataStatistics = () => import('../components/dataStatistics.vue')
const UpDataImg = ()=> import('../components/UpDataImg.vue')

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/login'
}, {
  path: '/login',
  component: Login
}, {
  path: '/home',
  component: Home,
  redirect: '/ProductList',
  children: [
  { path: '/productList', component: ProductList, }, 
  { path: '/productRentalAndSales', component: ProductRentalAndSales, }, 
  { path: '/sessionGenerationDesignPrinciples', component: SessionGenerationDesignPrinciples, }, 
  { path: '/venueManagement', component: VenueManagement, }, 
  { path: '/venueReservation', component: VenueReservation, },
  { path: '/mainOrder', component: MainOrder, }, 
  { path: '/rentalAndSalesOrder', component: RentalAndSalesOrder, }, 
  { path: '/venueReservationOrder', component: VenueReservationOrder, }, 
  { path: '/membershipCardPurchaseOrder', component: MembershipCardPurchaseOrder, }, 
  { path: '/refundManagement', component: RefundManagement, }, 
  { path: '/couponManagement', component: CouponManagement, }, 
  { path: '/membershipCardManagement', component: MembershipCardManagement, }, 
  { path: '/membershipCardList', component: MembershipCardList, }, 
  { path: '/fundsFlow', component: FundsFlow, }, 
  { path: '/userList', component: UserList, }, 
  { path: '/dataStatistics', component: DataStatistics, },
  { path: '/UpDataImg', component: UpDataImg, },
]
}]

const router = new VueRouter({
  mode: 'hash',
  base: './static/admin/',
  routes
})

// 挂载路由导航守卫，控制页面访问权限
router.beforeEach((to, from, next) => {
  // to将要访问的路径，from从哪里跳转而来，next()表示放行，next('/login')强制跳转
  // 如果访问login，放行
  if (to.path === '/login') return next()
  // 获取token
  const token = window.sessionStorage.getItem('token')
  // 如果token不存在，跳转login页面
  if (!token) return next('/login')
  // token存在放行
  next()
})

export default router
